
<template>
    <van-address-list
        v-if="action == 'elsect'"
        v-model="active"
        :list="list"
        default-tag-text="默认"
        @add="AddressAdd"
        @edit="AddressEdit"
        @select="OnAddelect"
    />
    <van-address-list 
        v-else
        v-model="active"
        :list="list"
        default-tag-text="默认"
        @add="AddressAdd"
        @edit="AddressEdit"
        @select="AddressCheck"
    />
</template>

<script setup>

// import { onMounted } from 'vue';

    const {proxy} = getCurrentInstance()

    let business = proxy.$business.value //拿到储存的数据

    //接收传递过来的参数 有就拿没有就给空
    let action = proxy.$route.query.action ? proxy.$route.query.action : ''
    //点进去就是打勾的状态  否则就是不打勾的状态
    action = ref(action)

    onMounted(() => {
        Addressdata()
    })

    let active = ref(0)

    //收获地址列表
    let list = reactive([])

    const Addressdata = async () => 
    {
        var result = await proxy.$POST({
            url:'/address/index',
            //busid = business.id
            params: {busid: business.id}
        })

        // console.log(result)

        if(result.code == 0)
        {
            proxy.$fail(result.msg)
            return false
        }

        //map((item) 循环每一个数据 
        result.data.map((item) => {
            //选中默认的状态
            if(item.status == 1) 
            {
                active.value = item.id
            }
            // console.log(active.value = item.status)
            //数据渲染
            //看vant的业务组件  地址列表
            list.push({
                id: item.id,
                name: item.consignee,
                tel: item.mobile,
                address:`${item.region_text} ${item.address}`,
                isDefault: item.status == '1' ? true : false,
            })
        })
    }

    //切换默认地址
    const AddressCheck = async (item) => {
        console.log(item)
        var result = await proxy.$POST({
            url:'/address/check',
            params:{
                busid:proxy.$business.value.id,
                addressid:item.id
            }
        })

        if(result.code==0)
        {
            proxy.$fail(result.msg)
            return false
        }

        // console.log(list)

        list.map((value) => value.isDefault = value.id == item.id ? true : false)



    }

    //收获地址添加
    const AddressAdd = async () => proxy.$router.push('/address/add')

    //收获地址的编辑
    const AddressEdit = async (item) => {
        proxy.$router.push({
            path:'/address/edit',
            query:{addressid: item.id}
        })

        console.log(item)

    }

    //确认订单收货地址 选择收货地址
    const OnAddelect = async(item) => {
        // console.log(item)

        //自定义一个属性字段
        item.region_text = item.address

        proxy.$address.value = item
        console.log(proxy.$address.value)

        proxy.$router.go(-1)

    }
</script>